<?php
/**
 * This library is a wrapper for the object-oriented version of the module. It
 * can be used as-is in any object-oriented module; simply search & replace
 * 'resourcepage' with the desired module name. 
 *
 * @copyright &copy; 2006 The Open University
 * @author s.marshall@open.ac.uk
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package resourcepage
 *//** */

// Include object-oriented version
require_once('module_resourcepage.php');
global $CFG;
require_once($CFG->libdir.'/ddllib.php');


function resourcepage_add_instance(&$resourcepage) {
    global $CFG;
    
    try {
        return module_resourcepage::$inst->add_instance($resourcepage);
    } catch (Exception $e) {
        // At later date, use of exceptions allows for reporting 
        // more specific error messages 
        return false;
    }
}


function resourcepage_update_instance($resourcepage) {
    global $CFG;
    
    try {
        module_resourcepage::$inst->update_instance($resourcepage);
        return true;
    } catch (Exception $e) {
        return false;
    }
    
}


function resourcepage_delete_instance($id) {
    global $CFG;
    
    try {
        module_resourcepage::$inst->delete_instance($id);
        return true;
    } catch (Exception $e) {
        return false;
    }
    
}

function resourcepage_get_participants($id) {
    global $CFG;
//Returns the users with data in one resource
//(NONE, but must exist on EVERY mod !!)
//(WHY?!? Nothing else needs to!!!)

    try {
        return module_resourcepage::$inst->get_instance_participants($id);
    } catch (Exception $e) {
        return false;
    }

}

/**
 * Function called after module has been installed
 */
function resourcepage_after_install() {

    global $CFG;
    if($CFG->dbtype=='postgres7') {
        // For Postgres, add a check constraint that prevents illegal rows of a type we've seen
        $result=execute_sql('ALTER TABLE '.$CFG->prefix.'resourcepage_items ADD CONSTRAINT '.
            $CFG->prefix.'resourcepage_itemtype_ck CHECK(basedonitemid IS NOT NULL OR itemtype IS NOT NULL)');

        // For Postgres (existing installation), allow weblinks >= 256 chars (up to 511)
        $result .= execute_sql('ALTER TABLE '.$CFG->prefix.'resourcepage_items ALTER COLUMN weblink TYPE VARCHAR(511)');
    }

}

function resourcepage_upgrade_submodules() {
    global $CFG;
/// This function does not upgrade submodules. However, it's the only
/// one you can override that gets called when a module is either
/// installed or updated. We need it in order to call the install()
/// hook. (Update is done via the php file in db/.)

    // See if module was already installed
    try {    
        $result = true;
        // Check if default canned messages have been added
        if (!count_records('resourcepage_canned')) {
            $tw=new transaction_wrapper();

            // Insert default canned messages
            $rowdata = new stdClass;
            $rowdata->canned = 'downloadsize';
            $rowdata->displayname = 'Download size warning';
            $rowdata->html = 'Please note: Download times will vary depending on file size and internet connection speed. With a 56 kb/s modem it will take about five minutes to download a 1 MB (1024 KB) file.';
            $result = insert_record('resourcepage_canned', $rowdata, false);

            $rowdata = new stdClass;
            $rowdata->canned = 'accessfiles';
            $rowdata->displayname = 'Inaccessible files warning';
            $gifalt = 'inaccessible.gif" alt="X icon, read as \'not accessible to screen readers\'" /> Files marked with a red X are not accessible to those using screen reading software.';
            $rowdata->html = addslashes('<img src="%%FLAGICON%%/'.$gifalt);
            if(!file_exists($CFG->dirroot.'/mod/resourcepage/pix/i/inaccessible.gif')) {
                $rowdata->html = addslashes('<img src="../../pix/resourcepage_flags/'.$gifalt);
            }
            $result &= insert_record('resourcepage_canned', $rowdata, false);

            $rowdata = new stdClass;
            $rowdata->canned = 'printed';
            $rowdata->displayname = 'Printed item information';
            $rowdata->html = addslashes('You do not need to print out files marked \'printed material\'; paper versions of this material will be sent to you in a course mailing.');
            $result &= insert_record('resourcepage_canned', $rowdata, false);

            $tw->complete($result);

            // TODO Handle any errors i.e. !$result
        }

        // Do what function appears to be designed for...
        // Upgrade submodules i.e. resourcepage extensions
        module_resourcepage::$inst->create_extensions_list();
    } catch(Exception $e) {
        // There is no nice way to report an error from this function
    }
    
}


function resourcepage_cron() {
    try {
        return module_resourcepage::$inst->cron();
    } catch(Exception $e) {
        mtrace('Unexpected exception in resourcepage cron: '.
            $e->getMessage().' -- '.$e->getFile().':'.$e->getLine()."\n");
    }
}

function resourcepage_process_file_request($courseid,$args) {    
        global $DASHBOARD_COUNTER;
        $DASHBOARD_COUNTER = DASHBOARD_RESOURCEPAGE_DOWNLOAD;
        
        // This calls error() if it fails, thus triggering autocounting of fail
        $result=module_resourcepage::$inst->process_file_request($courseid,$args);
        
        // We need to track the download now as if we wait until it finished
        // the time results will be useless (and user cancel might break it, 
        // etc)
        dashboard::page_complete();
        
        return $result;
}

function resourcepage_delete_course($course) {
    module_resourcepage::$inst->delete_course($course);
}

function resourcepage_get_types() {
    $types = array();

    $type = new object();
    $type->modclass = MOD_CLASS_RESOURCE;
    $type->type = "resourcepage";
    $type->typestr = get_string('resourcedropdown', 'resourcepage');
    $types[] = $type;

    return $types;
}

/**
 * @param string $feature FEATURE_xx constant for requested feature
 * @return mixed True if module supports feature, null if doesn't know
 */
function resourcepage_supports($feature) {
    switch($feature) {
        case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
        default: return null;
    }
}

function resourcepage_is_moddata_trusted() {
    // Was added in Moodle 1.9 series to force download for all module files
    // unless you return true.
    // Really we should just return true as all resourcepage files are trusted,
    // however we probably want the 'force download' behaviour except for PDFs
    $relativepath = get_file_argument('file.php');
    return preg_match('/\.pdf$/', $relativepath);
}

?>
